<?php
/**
 * Created by 张彪.
 * User: 小马出行
 * Date: 2018/1/20
 * Time: 13:13
 */
namespace app\admin\model;

use think\Db;
use think\Model;
class AdminModel extends Model
{

    public function getAdminInfo($map,$field='*',$flag=true,$order='admin_id desc',$page=1,$limit=10)
    {
        return $flag ? Db::name('admin')->where($map)->field($field)->page($page,$limit)->order($order)->select()
            : Db::name('admin')->where($map)->field($field)->order($order)->find();
    }

    public function adminCount($map)
    {
        return Db::name('admin')->where($map)->count();
    }

    /**
     * 查询当前管理所属级别
     * admin_type 1 全国 2 省级 3 市级 4 区域
     */
    public function getRoleArea(){
        $result = [];
        $adminInfo  = Db::name('admin')->where(['admin_id'=>session('admin_id')])->field('admin_type,region')->find();
        if(!empty($adminInfo['admin_type']) && $adminInfo['admin_type'] > 1){
            $adminInfo['region']  = json_decode($adminInfo['region'],true);
            if($adminInfo['admin_type'] == 2){
                $result['province'] = $adminInfo['region'][$adminInfo['admin_type']];
                //查询这个省份下面的所有城市
                $result['city']     = Db::name('region')->where(['parent_id'=>['in',implode(',',$result['province'])]])->column('region_id');
            }elseif($adminInfo['admin_type'] == 3){
                $result['province'] = $adminInfo['region'][$adminInfo['admin_type']-1];
                //查询城市
                $result['city']     = array_reduce($adminInfo['region'][$adminInfo['admin_type']], 'array_merge', array());
            }elseif ($adminInfo['admin_type'] == 4){
                $result['province'] = $adminInfo['region'][$adminInfo['admin_type']-2];
                $result['city']     = array_reduce($adminInfo['region'][$adminInfo['admin_type']-1], 'array_merge', array());
                $result['county']   = array_reduce($adminInfo['region'][$adminInfo['admin_type']], 'array_merge', array());
            }
        }
        return $result;
    }

}
